// <copyright file="Variable.cs" company="Codeplex project Eco.Contrib"> 
// The MIT License (MIT) 
// </copyright> 
// <author>Peter Buchmann</author> 

namespace Eco.Contrib
{
    using Eco.ObjectRepresentation;
    using Eco.UmlCodeAttributes;

    /// <summary>
    /// Variable: Variable can be used in other ESL expressions
    /// </summary>
    public partial class Variable
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="Variable"/> class.
        /// </summary>
        /// <param name="serviceProvider">The service provider.</param>
        /// <param name="variableName">Name of the variable.</param>
        [UmlElement(Id = "7df708b3-6c61-4ff3-aef4-7c95ff1c9ca2")]
        public Variable(IEcoServiceProvider serviceProvider, string variableName)
            : this(serviceProvider)
        {
            this.Name = variableName;
        }

        /// <summary>
        /// Sets the specified variable list.
        /// </summary>
        /// <param name="variableList">The variable list.</param>
        /// <param name="value">The value.</param>
        [UmlElement(Id = "9ebecab7-165c-4c8b-b8f4-289679782fda")]
        public void Set(IModifiableVariableList variableList, IElementProvider value)
        {
            IExternalVariable variable = variableList[this.Name];
            if (variable != null)
            {
                variableList.Remove(this.Name);
            }

            variableList.Add(this.Name, value.Element);
        }
    }
}